<template>
  <div class="view-wrapper">
    <div class="head">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="">
          <el-input placeholder="活动名称/优惠券名称/手机号/核销地点" style="width: 280px" v-model="formInline.query"
            class="input-with-select">
          </el-input>
        </el-form-item>
        <el-form-item label="优惠券类型：">
          <el-select v-model="formInline.couponType" placeholder="请选择">
            <el-option v-for="item in openLs" :key="item.id" :label="item.value" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="领券时间：">
          <el-date-picker v-model="date" type="daterange" value-format="yyyy-MM-dd" range-separator="至"
            start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="reset">重置</el-button>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="listBox">
      <div class="listBox_head">
        <div>
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane :label="'全部'+ '（'+ totalInfo.total + '）'" name="first"></el-tab-pane>
            <el-tab-pane :label="'未使用'+ '（'+ totalInfo.unUsedNum + '）'" name="second"></el-tab-pane>
            <el-tab-pane :label="'已使用'+ '（'+ totalInfo.usedNum + '）'" name="third"></el-tab-pane>
            <el-tab-pane :label="'已过期'+ '（'+ totalInfo.overDueNum + '）'" name="fourth"></el-tab-pane>
          </el-tabs>
        </div>
        <!-- <el-button type="primary">导出</el-button> -->
        <export-excel-button title="领券明细" action="/customer/platform/coupon/list/export" :params="ExportParams" />
      </div>
      <ex-table ref="table" :loading="loading" :table-column="tableColumn" :table-data="tableData"
        @pageSizeChange="handleSizeChange" @currentPageChange="handleCurrentChange">
        <!-- <template v-slot:operate="{ scope }">
          <el-button @click="edit(scope.row.shopId)" size="small">编辑</el-button>
          <el-button type="primary" size="small">复制链接</el-button>
        </template> -->
      </ex-table>
    </div>
  </div>
</template>
<script>
  // components
  import {
    couponReceiveLs,
    couponStatistics,
    adminDict
  } from "@/api/couponManagement";
  import handlePageMixin from "@/mixins/handlePageMixin";
  import ExportExcelButton from "@/components/ExportExcelButton/index";
  export default {
    //   name: "",
    components: {
      ExportExcelButton,
    },
    mixins: [handlePageMixin],
    data() {
      return {
        activeName: 'first',
        loading: false,
        totalInfo: {
          total: '',
          unUsedNum: '',
          usedNum: '',
          overDueNum: '',
        },
        formInline: {
          query: "", //活动名称/优惠券名称/手机号
          couponType: "", //优惠券类型;(1 满减券 2 折扣券 3 礼品券) platform_coupon_type
          status: '', //使用状态;1 未使用 2 已使用 3 已过期' customer_platform_coupon_status
        },
        openLs: [],
        typeLs: [],
        date: [],
        type: "",
        tableData: [],
        tableColumn: [{
            prop: "couponCode",
            label: "券码"
          },
          {
            prop: "platformName",
            label: "领券渠道",
            width: 150
          },
          {
            prop: "activityName",
            label: "活动名称"
          },
          {
            prop: "couponName",
            label: "优惠券名称",
            width: 130
          },
          {
            prop: "couponTypeDict",
            label: "优惠券类型",
            width: 130
          },
          {
            prop: "validityTypeDict",
            label: "优惠券有效期",
            width: 140
          },
          {
            prop: "preferentialCondition",
            label: "优惠券条件",
            width: 130
          },
          {
            prop: "preferentialDiscount",
            label: "优惠力度"
          },
          {
            prop: "nickName",
            label: "领券人",
            width: 160
          },
          {
            prop: "phoneNumber",
            label: "手机号",
            width: 160
          },
          {
            prop: "createTime",
            label: "领券时间",
            width: 160
          },
          {
            prop: "statusDict",
            label: "优惠券状态",
            width: 130
          },
        ],
      };
    },
    created() {
      this.getTotal();
      this.getTypeList();
      this.getList();
    },
    computed: {
      ExportParams() {
        const {
          id: ticketId
        } = this;
        const params = {
          ...this.pageOptions,
          ...this.formInline,
          startTime: this.date[0],
          endTime: this.date[1],
        };
        return params;
      },
    },
    methods: {
      getTypeList() {
        //获取字典
        const params = {
          dictType: "platform_coupon_type",
        };
        adminDict(params).then((res) => {
          this.openLs = res.result[0].dictList;
        });
      },
      getTotal() {
        //领券明细 统计
        const params = {
          ...this.pageOptions,
          ...this.formInline,
          startTime: this.date[0],
          endTime: this.date[1],
        };
        couponStatistics(params).then((res) => {
          this.totalInfo.total = res.result.total;
          this.totalInfo.unUsedNum = res.result.unUsedNum;
          this.totalInfo.usedNum = res.result.usedNum;
          this.totalInfo.overDueNum = res.result.overDueNum
        });
      },
      handleClick(tab, event) { //选择
        let index = tab.index
        console.log(index);
        if (index == '1') {
          this.formInline.status = '1'
        } else if (index == '2') {
          this.formInline.status = '2'
        } else if (index == '3') {
          this.formInline.status = '3'
        } else {
          this.formInline.status = ''
        }
        this.getList()
      },
      onSubmit() {
        //查询
        console.log(this.formInline);
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getTotal();
        this.getList();
      },
      reset() {
        this.formInline = {};
        this.formInline.status = ''
        this.date = [];
        this.activeName = 'first'
        this.pageOptions.pageNum = 1;
        this.$refs.table.pageOptions.pageNum = 1;
        this.getTotal();
        this.getList();
      },
      getList() {
        const params = {
          ...this.pageOptions,
          ...this.formInline,
          startTime: this.date[0],
          endTime: this.date[1],
        };
        this.loading = true;
        couponReceiveLs(params)
          .then((res) => {
            const {
              total,
              records
            } = res.result;
            records.forEach((item) => {
              if (item.validityType == '2') {
                item.validityTypeDict = item.afterReceivingDay + '天后生效' + item.effectiveDays + '天'
              } else if (item.validityType == '3') {
                item.validityTypeDict = item.validityStartTime + '至' + item.validityEndTime
              }
              
            });
            this.$refs.table.pageOptions.total = total;
            this.tableData = records;
          })
          .finally(() => {
            this.loading = false;
          });
      },
      lookDetail(id) {
        this.$router.push({
          path: "/clientManagement/shopDetail",
          query: {
            id: id
          },
        });
      },
    },
  };

</script>
<style lang="scss">
  .el-card-title {
    font-size: 20px;
  }

</style>
<style lang="scss" scoped>
  .head {
    box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px 0;
  }

  .el-form--inline .el-form-item {
    margin-right: 30px;
  }

  .listBox {
    box-shadow: 0px 4px 12px 0px rgb(1 41 99 / 6%);
    background-color: #fff;
    border-radius: 8px;
    //   height: 120px;
    padding: 20px 20px;
    margin-top: 10px;
  }

  .table_list {
    display: flex;
    align-items: center;
    // border: 1px solid #DCDFE6;
    border-radius: 7px;
    width: 220px;
    margin-bottom: 10px;
  }

  .tableBox {
    width: calc(100% / 3);
    height: 36px;
    text-align: center;
    line-height: 36px;
    color: #303133;
    font-size: 14px;
    border: 1px solid #dcdfe6;
    border-right: 0;
  }

  .tableBox:first-child {
    border-radius: 7px 0px 0px 7px;
  }

  .tableBox:last-child {
    border-right: 1px solid #dcdfe6;
    border-radius: 0px 7px 7px 0px;
  }

  .spchoosetext {
    background-color: #fff4e6;
    color: #ff9300;
    // border: 1px solid #ff9300;
  }

  .listBox_head {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;
  }

  .el-select .el-input {
    width: 130px;
  }

  .input-with-select .el-input-group__prepend {
    background-color: #fff;
  }

  .head ::v-deep.el-input-group__prepend {
    width: 110px !important;
  }

</style>
